package cn.fsw.pethome.org.controller;

import cn.fsw.pethome.annotations.PreAuthorize;
import cn.fsw.pethome.org.domain.Department;
import cn.fsw.pethome.org.query.DepartmentQuery;
import cn.fsw.pethome.org.service.IDepartmentService;
import cn.fsw.pethome.util.AjaxResult;
import cn.fsw.pethome.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/list")
    @ApiOperation(value = "查询全部部门信息" )
    public List<Department> selectAll(){
        return departmentService.selectAll();
    }


    @PreAuthorize(name = "部门列表",value = "dept:pagelist")
    @PostMapping("/pageList")
    @ApiOperation(value = "高级查询和分页" )
    public PageList<Department> pageSelevtAll(@RequestBody DepartmentQuery query){

        return departmentService.pageSelevtAll(query);
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department selectById(@PathVariable("id") Long id){
        return null;
    }

    @PreAuthorize(name = "部门保存",value = "dept:sava")
    @PutMapping("/department")
    @ApiOperation(value = "新增或者修改部门信息" )
    public AjaxResult update(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }else {
            departmentService.update(department);
            }
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @PreAuthorize(name = "部门删除",value = "dept:delete")
    @DeleteMapping("/department/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PatchMapping("/batchDelete")
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @GetMapping("/deptTree")
    @ApiOperation(value = "部门树" )
    public List<Department> deptTree(){
       return departmentService.deptTree();
    }
}
